package org.mspring.mlog.common;

import org.apache.commons.lang3.StringUtils;
import org.mspring.mlog.MarkdownUtils;
import org.mspring.mlog.entity.ArticleEntity;
import org.mspring.mlog.entity.CategoryEntity;
import org.mspring.mlog.entity.TagEntity;
import org.mspring.mlog.common.constants.ContentFormat;
import org.mspring.mlog.support.ServiceContext;
import org.mspring.nbee.common.utils.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Gao Youbo
 * @since 2015-01-04 14:17
 */
public class ArticleDto {
    private Long id;
    private CategoryEntity category;
    private String title;
    private String content;
    private List<TagEntity> tagList;
    private List<ContentImage> imageList;
    private int viewCount;
    private Date createTime;
    private long createUser;
    private String createUserName;
    private Date updateTime;
    private long updateUser;
    private String updateUserName;

    public static ArticleDto fromArticle(ArticleEntity article, ServiceContext serviceContext) {
        if (article == null) {
            return null;
        }
        String content = article.getContent();
        if (StringUtils.equalsIgnoreCase(article.getFormat(), ContentFormat.MARKDOWN)) {
            // 转换markdown样式
            content = MarkdownUtils.format(content);
        }
        CategoryEntity category = serviceContext.getCategoryEntityService().findByIdWithCache(article.getCategory());
        List<TagEntity> tagList = serviceContext.getTagCacheService().getArticleTagList(article.getId());
        ArticleDto dto = new ArticleDto();
        dto.setId(article.getId());
        dto.setCategory(category);
        dto.setTitle(article.getTitle());
        dto.setContent(content);
        dto.setTagList(tagList);
        dto.setImageList(ContentImage.fromJson(article.getImageList()));
        dto.setViewCount(serviceContext.getArticleViewCountService().getViewCount(article.getId()));
        dto.setCreateTime(article.getCreateTime());
        dto.setCreateUser(article.getCreateUser());
        dto.setCreateUserName(article.getCreateUserName());
        dto.setUpdateTime(article.getUpdateTime());
        dto.setUpdateUser(article.getUpdateUser());
        dto.setUpdateUserName(article.getUpdateUserName());
        return dto;
    }

    public static List<ArticleDto> fromArticles(List<ArticleEntity> articles, ServiceContext serviceContext) {
        List<ArticleDto> ret = new ArrayList<>();
        if (CollectionUtils.isEmpty(articles)) {
            return ret;
        }
        for (ArticleEntity article : articles) {
            ret.add(fromArticle(article, serviceContext));
        }
        return ret;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public CategoryEntity getCategory() {
        return category;
    }

    public void setCategory(CategoryEntity category) {
        this.category = category;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public List<TagEntity> getTagList() {
        return tagList;
    }

    public void setTagList(List<TagEntity> tagList) {
        this.tagList = tagList;
    }

    public List<ContentImage> getImageList() {
        return imageList;
    }

    public void setImageList(List<ContentImage> imageList) {
        this.imageList = imageList;
    }

    public int getViewCount() {
        return viewCount;
    }

    public void setViewCount(int viewCount) {
        this.viewCount = viewCount;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public long getCreateUser() {
        return createUser;
    }

    public void setCreateUser(long createUser) {
        this.createUser = createUser;
    }

    public String getCreateUserName() {
        return createUserName;
    }

    public void setCreateUserName(String createUserName) {
        this.createUserName = createUserName;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public long getUpdateUser() {
        return updateUser;
    }

    public void setUpdateUser(long updateUser) {
        this.updateUser = updateUser;
    }

    public String getUpdateUserName() {
        return updateUserName;
    }

    public void setUpdateUserName(String updateUserName) {
        this.updateUserName = updateUserName;
    }


}
